package com.caijinbang.study.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * FastJson配置,
 * 使用@JSONField(format = "yyyy-MM-dd HH:mm:ss")在get参数时候会转化成String类型
 *
 * @author caijb
 * @version 1.0.0
 * @date 2018-04-22 20:35
 */
@Configuration
public class FastJsonConfiguration extends WebMvcConfigurationSupport {

    /**
     * 消息转换
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //1、创建FastJson转换器
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //2、添加 fastJson 的配置信息，比如: 是否要格式化返回的Json数据；
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteNullListAsEmpty, SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.PrettyFormat);
        //3、在 Convert 中添加配置信息;
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //4、将 convert 添加到 converts 中;
        converters.add(fastJsonHttpMessageConverter);
    }
}
